package com.bwie.parkdemo.mvc;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.bwie.parkdemo.R;
import com.bwie.parkdemo.mvvm.ui.fragment.DepartmentFragment;
import com.bwie.parkdemo.mvvm.ui.fragment.UnDepartmentFragment;
import com.jaeger.library.StatusBarUtil;

/**
 * 人员管理activity
 * */
public class DepartmentActivity extends AppCompatActivity implements View.OnClickListener{

    private Button leftBtn,rightBtn;
    private DepartmentFragment left;
    private UnDepartmentFragment right;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_department);
        //导入沉浸式状态栏颜色
        StatusBarUtil.setColor(this,getResources()
                .getColor(com.bwie.lib_core.R.color.statuescolor),0);
        init();
    }

    private void init(){
        leftBtn = findViewById(R.id.department_left_btn);
        rightBtn = findViewById(R.id.department_right_btn);
        leftBtn.setOnClickListener(this);
        rightBtn.setOnClickListener(this);
        showFragment(R.id.department_left_btn);
    }

    @Override
    public void onClick(View v) {
        hideFragment();
        showFragment(v.getId());
    }

    //隐藏全部fragmetn逻辑
    private void hideFragment(){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (left!=null){
            ft.hide(left);
        }
        if (right!=null){
            ft.hide(right);
        }
        ft.commit();
    }

    //根据按钮点击选择对应fragment
    private void showFragment(int id){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (id == R.id.department_left_btn){
            if (left == null){
                left = new DepartmentFragment();
                ft.add(R.id.department_fg_group,left);
            }else {
                ft.show(left);
                left.refreshDatas();
            }
        }else {
            if (right == null){
                right = new UnDepartmentFragment();
                ft.add(R.id.department_fg_group,right);
            }else
                ft.show(right);
        }
        ft.commit();
    }
}
